/**
 * Created with IntelliJ IDEA.
 * User: jiang.li
 * Date: 13-10-23
 * Time: 上午11:01
 * To change this template use File | Settings | File Templates.
 */
var _idMap = {};
var _sendMsg = function (data, callback) {
    _idMap[data.id] = {
        data: data,
        callback: callback
    };// 记录消息以供成功后回调
    parent.window.webSocket_send(data);
}

window.AllDocCrt = function ($rootScope, $scope) {

    //组装查询参数
    $scope.queryParams || ( $scope.queryParams = {});
    $scope.formParams || ( $scope.formParams = {});
    $scope.msg || ( $scope.msg = {});
    $scope.queryParams.pid = "";
    $scope.type = "创建";

    //查询数据函数
    var loadData = function () {
        $.ajax({
            url: '/services/api/fileserver/file_info/listFile',
            type: "GET",
            data: $scope.queryParams,
            dataType: "json",
            success: function (data) {
                $scope.items = data.items;
                $.each($scope.items, function (i, item) {
                        item.icon = "/images/file/"+item.fileSuffix+".gif";
                        if(item.fileSuffix=="folder"){
                            item.name = item.fileName;
                            item.showMsg = false;
                            item.tips = "点击进入子目录";
                        }else{
                            item.tips = "点击开始下载";
                            item.showMsg = true;
                            item.name = item.fileName + "." + item.fileSuffix;
                        }
                });
                applyAngularScope($rootScope, $scope);
            }
        });
    }

    //保存文件夹
    var saveFolder = function(){
        $.ajax({
            url: "/services/api/fileserver/file_info/saveFile",
            type: "POST",
            data: JSON.stringify($scope.formParams),
            dataType: "json",
            success: function (data) {
                loadData();
                $scope.formParams = {};
            }
        });
    }


    //保存文件
    var saveFile = function(dataObj){

        $scope.formParams.fileType = "2";
        $scope.formParams.fileSuffix = dataObj.fileSuffix;
        $scope.formParams.fileName = dataObj.fileName;
        $scope.formParams.filePath = dataObj.filePath;
        $scope.formParams.fileSize = dataObj.fileSize;

        $.ajax({
            url: "/services/api/fileserver/file_info/saveFile",
            type: "POST",
            data: JSON.stringify($scope.formParams),
            dataType: "json",
            success: function (data) {
                loadData();
                $scope.formParams = {};
            }
        });
    }

    //初始化上传
    var initUpload = function () {
        $("#uploadify").uploadify({
            'uploader': '/res/plugin/uploadify/uploadify.swf',
            'script':"/fileserver/upload",
            'cancelImg': '/res/plugin/uploadify/cancel.png',
            'queueID': 'fileQueue',
            'auto': true,
            'buttonText': 'select',
            'simUploadLimit' : 1,
            'queueSizeLimit' : 1,
            'fileExt': '*.jpg;*.gif;*.jpeg;*.png;*.bmp;*.zip;*.rar;*.7z',
            onComplete: function(event, queueID, fileObj, response, data) {
                //转换为json对象
                var dataObj = eval("("+response+")");
                saveFile(dataObj);
            },
            onSelect:function(){
            },
            onError: function(event, queueID, fileObj) {
                alert("文件:" + fileObj.name + "上传失败");
            }
        });
    }

    //下载附件
    var down = function (fileId) {
        var url = "/services/api/fileserver/file_info/downFile/"+fileId;
        window.location.href = url;
    }

    //是目录就查询子目录，是文件就提示下载
    $scope.open = function (item) {
        $scope.formParams = {};
        if(item.fileType=="1"){
            $scope.queryParams.pid = item.id;
            $scope.formParams.pid = item.id;
            loadData();
        }else{
            down(item.id);
        }
    }

    //是目录就查询子目录，是文件就提示下载
    $scope.up = function (upId) {
        $scope.queryParams.pid = upId;
        loadData();
    }

    //删除文件或者文件夹
    $scope.del = function (item) {
        var fileType = item.fileType;
        var fileName = item.fileName;
        var fileSuffix = item.fileSuffix;
        var word = "";
        if(fileType=="1"){
            word = "确定要删除\""+fileName + "\"及其子目录吗？";
        }else{
            word = "确定要删除\""+fileName + "." + fileSuffix + "\"文件吗？";
        }
        if(confirm(word)){
            $.ajax({
                url: '/services/api/fileserver/file_info/delFile/'+item.id,
                type: "GET",
                success: function (data) {loadData();}
            });
        }
    }

    //显示根目录
    $scope.show = function (){
        $scope.queryParams.pid = "";
        loadData();
    }

    //组装修改表单
    $scope.edit = function(item){
        $scope.formParams.id = item.id;
        $scope.formParams.pid = item.pid;
        $scope.formParams.fileType = item.fileType;
        $scope.formParams.fileName = item.fileName;
        $scope.formParams.filePath = item.filePath;
        $scope.formParams.fileSuffix = item.fileSuffix;
        $scope.formParams.fileSize = item.fileSize;
        $scope.type = "保存";
    }

    //保存文件夹
    $scope.saveFolder = function(){
        if(!$scope.formParams.id){
            $scope.formParams.fileType = "1";
            $scope.formParams.fileSuffix = "folder";
            $scope.formParams.fileSize = "0";
        }
        var fileName = $scope.formParams.fileName;
        if(fileName){
            saveFolder();
        }else{
           alert("名称不能为空!");
        }
    }

    //上传文件
    $scope.uploadFile = function(){
        $('#uploadify').uploadifyUpload();
    }

    //上传取消
    $scope.clearFile = function(){
        $('#uploadify').uploadifyClearQueue();
    }

    //显示下载消息通知DIV
    $scope.showDiv = function(item){
        $scope.msg.fileName = item.fileName;
        $scope.msg.url = "/services/api/fileserver/file_info/downFile/" + item.id;
        $('#myModal').modal('show');

        $scope.msg.title = item.fileName;
        $scope.msg.content = $("#content").html();
        applyAngularScope($rootScope, $scope);
    }


    //获取所有有效的设备信息
    var deviceData = function(){
        $scope.deviceList = {};
        $.ajax({
            url: "/services/api/fileserver/file_info/listDevice",
            type: "GET",
            data: $scope.formParams,
            dataType: "json",
            success: function (data) {
                $scope.deviceList = data.items;
            }
        });
    }


    //组装下载通知内容
    var sendMsgData = function (deviceId) {
        var data = {
            id: genUUID(),
            type: "post",
            status: 0,
            path: "/systemCommTo",
            header: {
                to: deviceId
            },
            body: {
                path: '/msg/msg/sendText',
                type: 'put',
                body: {
                    title: $scope.msg.title,
                    content: $scope.msg.content
                }
            }
        };
        return data;
    }

    //推送文件下载通知
    $scope.sendMsg = function(){
        $scope.deviceList = {};
        $.ajax({
            url: "/services/api/fileserver/file_info/listDevice",
            type: "GET",
            data: $scope.formParams,
            dataType: "json",
            success: function (data) {
                $scope.deviceList = data.items;
                $.each($scope.deviceList, function (i, item) {
                    if (item.id) {
                        var data = sendMsgData(item.id);
                         _sendMsg(data, function () {});
                    }
                });
            }
        });
        $("#myModal").modal("hide");
        alert("消息已经全部发送!");
    }

    //首次加载数据
    loadData();
    initUpload();

}
